package com.zg.scf.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zg.common.core.R;
import com.zg.common.core.dao.scf.ImgCategoryDao;
import com.zg.common.core.dao.scf.entity.*;
import com.zg.common.core.service.PageBaseService;
import com.zg.scf.domain.vo.ImgCategoryVo;
import com.zg.scf.domain.vo.ImgConditionVo;
import com.zg.scf.domain.vo.ImgConfigVo;
import com.zg.scf.feign.ThirdFeign;
import com.zg.scf.service.CompanyService;
import com.zg.scf.service.ImgCategoryScoreService;
import com.zg.scf.service.ImgCategoryService;
import com.zg.scf.service.ImgConditionService;
import com.zg.third.api.domain.vo.QccLandMergeCheckVo;
import com.zg.third.api.domain.vo.QccSeriousIllegalCheckVo;
import com.zg.third.api.domain.vo.QccTaxCreditVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.Period;
import java.time.ZoneId;
import java.util.List;
import java.util.Objects;

/**
 * <p>Description: </p>
 *
 * @author zhanfeng.liang
 * @since 2025/5/22 10:47
 */



@Service
@Slf4j
public class ImgCategoryServiceImpl extends PageBaseService<ImgCategory, ImgCategoryDao> implements ImgCategoryService {

    @Resource
    private CompanyService companyService;

    @Resource
    private ImgConditionService imgConditionService;

    @Resource
    private ImgCategoryScoreService imgCategoryScoreService;

    @Resource
    private ThirdFeign thirdFeign;



    @Override
    public void removeByConfigId(Long id) {
        LambdaQueryWrapper<ImgCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ImgCategory::getConfigId, id);
        baseDao.remove(queryWrapper);
    }

    @Override
    public List<ImgCategory> listByConfigId(Long id) {
        ImgCategory queryWrapper = new ImgCategory();
        queryWrapper.setConfigId(id);
        return list(queryWrapper);
    }

    @Override
    public List<ImgCategory> listByPid(Long id) {
        ImgCategory queryWrapper = new ImgCategory();
        queryWrapper.setPid(id);
        return list(queryWrapper);
    }

    @Override
    public List<ImgCategory> listByPids(List<Long> ids) {
        ImgCategory queryWrapper = new ImgCategory();
        queryWrapper.setPids(ids);
        return list(queryWrapper);
    }

    @Override
    public void compute(ImgConfigVo imgConfigVo) {
        List<ImgCategoryVo> pCategoryVoList = imgConfigVo.getCategoryList();
        for(int j=0;j<pCategoryVoList.size();j++){
            ImgCategoryVo pCategoryVo = pCategoryVoList.get(j);
            List<ImgCategoryVo> subCategoryVoList = pCategoryVo.getSubList();
            List<ImgConditionScore> imgConditionScoreList = Lists.newArrayList();
            for(int k=0;k<subCategoryVoList.size();k++){
                ImgCategoryVo subCategoryVo = subCategoryVoList.get(k);
                ImgConditionScore  imgConditionScore=compute(imgConfigVo,pCategoryVo,subCategoryVo);
                imgConditionScoreList.add(imgConditionScore);
            }
            BigDecimal totalScore = imgConditionScoreList.stream()
                    .filter(Objects::nonNull)
                    .map(score -> {
                        BigDecimal value = score.getScore();
                        return value != null ? value : BigDecimal.ZERO;
                    })
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            ImgCategoryScore imgCategoryScore = new ImgCategoryScore();
            imgCategoryScore.setCategoryId(pCategoryVo.getId());
            imgCategoryScore.setCompanyId(imgConfigVo.getCompanyId());
            imgCategoryScore.setTargetCompanyId(imgConfigVo.getTargetCompanyId());
            imgCategoryScore.setScore(totalScore);
            imgCategoryScore.setConfigId(imgConfigVo.getId());
            imgCategoryScoreService.add(imgCategoryScore);
        }
    }

    private ImgConditionScore compute(ImgConfigVo imgConfig, ImgCategoryVo pImgCategory, ImgCategoryVo subImgCategory) {
        Company company = companyService.getById(imgConfig.getTargetCompanyId());
        Long companyId = imgConfig.getCompanyId();
        Long targetCompanyId = imgConfig.getTargetCompanyId();

        ImgConditionScore imgConditionScore=null;
        switch (subImgCategory.getTitle()) {
            case "行业分类":
                 imgConditionScore=imgConditionService.computeDic(imgConfig,pImgCategory,subImgCategory,company.getIndustryType());
                 //imgConditionScoreList.add(imgConditionScore);
                // 行业分类逻辑处理
                break;
            case "公司性质":
                imgConditionScore=imgConditionService.computeDic(imgConfig,pImgCategory,subImgCategory,company.getCompanyNature());
                //imgConditionScoreList.add(imgConditionScore);
                // 公司性质逻辑处理
                break;
            case "存续时间":
                LocalDate beginDate = company.getBizBegin().toInstant()
                        .atZone(ZoneId.systemDefault())
                        .toLocalDate();
                LocalDate endData = company.getBizBegin().toInstant()
                        .atZone(ZoneId.systemDefault())
                        .toLocalDate();

                // 计算两个日期的差距
                Period period = Period.between(beginDate, endData);

                // 获取相差的年份
                int years = period.getYears();
                imgConditionScore=imgConditionService.computeTimes(imgConfig,pImgCategory,subImgCategory,years);
                //imgConditionScoreList.add(imgConditionScore);
                // 存续时间逻辑处理
                break;
            case "实缴资本":
                imgConditionScore=imgConditionService.computeTimes(imgConfig,pImgCategory,subImgCategory,company.getCapitalUnit());
                //imgConditionScoreList.add(imgConditionScore);
                // 实缴资本逻辑处理
                break;
            case "信息核查":
                // 信息核查逻辑处理
                break;
            case "纳税信用":
                String taxCredit = "";
                List<QccTaxCreditVo> qccTaxCreditVos =thirdFeign.taxCredit(company.getCreditCode());

                if(!qccTaxCreditVos.isEmpty()){
                    taxCredit=qccTaxCreditVos.get(0).getLevel();
                }
                imgConditionScore=imgConditionService.computeDic(imgConfig,pImgCategory,subImgCategory,taxCredit);
                //imgConditionScoreList.add(imgConditionScore);
                // 纳税信用逻辑处理
                break;
            case "国有土地受让":
                Integer landMergeCheck =thirdFeign.landMergeCheck(company.getCreditCode());
                imgConditionScore=imgConditionService.computeTimes(imgConfig,pImgCategory,subImgCategory,landMergeCheck);
                //imgConditionScoreList.add(imgConditionScore);
                // 国有土地受让逻辑处理
                break;
            case "抽查问题":
                // 抽查问题逻辑处理
                break;
            case "供应商数":
                Integer supplier =thirdFeign.supplier(company.getCreditCode());
                imgConditionScore=imgConditionService.computeTimes(imgConfig,pImgCategory,subImgCategory,supplier);
                //imgConditionScoreList.add(imgConditionScore);
                // 供应商数逻辑处理
                break;
            case "客户数量":
                // 客户数量逻辑处理
                Integer customer =thirdFeign.supplier(company.getCreditCode());
                imgConditionScore=imgConditionService.computeTimes(imgConfig,pImgCategory,subImgCategory,customer);
                //imgConditionScoreList.add(imgConditionScore);
                break;
            case "产权交易":
                // 产权交易逻辑处理
                break;
            case "资产拍卖":
                // 资产拍卖逻辑处理
                break;
            case "严重违法":
                // 严重违法逻辑处理
                QccSeriousIllegalCheckVo seriousIllegalCheckVo =thirdFeign.seriousIllegalCheck(company.getCreditCode());
                QccSeriousIllegalCheckVo.Info[] infos=seriousIllegalCheckVo.getData();
                imgConditionScore=imgConditionService.computeTimes(imgConfig,pImgCategory,subImgCategory,infos.length);
                //imgConditionScoreList.add(imgConditionScore);
                break;
            case "被告立案":
                // 被告立案逻辑处理
                Integer caseFilingCheck =thirdFeign.caseFilingCheck(company.getCreditCode());
                imgConditionScore=imgConditionService.computeTimes(imgConfig,pImgCategory,subImgCategory,caseFilingCheck);
                //imgConditionScoreList.add(imgConditionScore);
                break;
            case "开庭公告":
                Integer courtNoticeCheckVo =thirdFeign.courtAnnoCheck(company.getCreditCode());
                imgConditionScore=imgConditionService.computeTimes(imgConfig,pImgCategory,subImgCategory,courtNoticeCheckVo);
                //imgConditionScoreList.add(imgConditionScore);
                // 开庭公告逻辑处理
                break;
            case "终本案件":
                // 终本案件逻辑处理
                Integer endExecuteCaseCheck =thirdFeign.endExecuteCaseCheck(company.getCreditCode());
                imgConditionScore=imgConditionService.computeTimes(imgConfig,pImgCategory,subImgCategory,endExecuteCaseCheck);
                //imgConditionScoreList.add(imgConditionScore);
                break;
            case "司法拍卖":
                // 司法拍卖逻辑处理
                Integer judicialSaleCheck =thirdFeign.judicialSaleCheck(company.getCreditCode());
                imgConditionScore=imgConditionService.computeTimes(imgConfig,pImgCategory,subImgCategory,judicialSaleCheck);
                //imgConditionScoreList.add(imgConditionScore);
                break;
            case "司法案件":
                // 司法案件逻辑处理
                break;
            case "行政处罚":
                // 行政处罚逻辑处理
                Integer adminPenaltyCheck =thirdFeign.adminPenaltyCheck(company.getCreditCode());
                imgConditionScore=imgConditionService.computeTimes(imgConfig,pImgCategory,subImgCategory,adminPenaltyCheck);
                //imgConditionScoreList.add(imgConditionScore);
                break;
            case "环保处罚":
                Integer envPunishmentCheck =thirdFeign.envPunishmentCheck(company.getCreditCode());
                imgConditionScore=imgConditionService.computeTimes(imgConfig,pImgCategory,subImgCategory,envPunishmentCheck);
                //imgConditionScoreList.add(imgConditionScore);
                // 环保处罚逻辑处理
                break;
            case "公司催告":
                Integer publishNoticeCheck =thirdFeign.publishNoticeCheck(company.getCreditCode());
                imgConditionScore=imgConditionService.computeTimes(imgConfig,pImgCategory,subImgCategory,publishNoticeCheck);
                //imgConditionScoreList.add(imgConditionScore);
                // 公司催告逻辑处理
                break;
            case "惩戒名单":
                // 惩戒名单逻辑处理
                Integer disciplinaryCheck =thirdFeign.disciplinaryCheck(company.getCreditCode());
                imgConditionScore=imgConditionService.computeTimes(imgConfig,pImgCategory,subImgCategory,disciplinaryCheck);
                //imgConditionScoreList.add(imgConditionScore);
                break;
            case "专利信息":
                Integer patent =thirdFeign.patent(company.getCreditCode());
                imgConditionScore= imgConditionService.computeTimes(imgConfig,pImgCategory,subImgCategory,patent);
                //imgConditionScoreList.add(imgConditionScore);
                // 专利信息逻辑处理
                break;
            case "软件著作":
                // 软件著作逻辑处理
                Integer copyRight =thirdFeign.copyRight(company.getCreditCode());
                imgConditionScore=imgConditionService.computeTimes(imgConfig,pImgCategory,subImgCategory,copyRight);
                //imgConditionScoreList.add(imgConditionScore);
                break;
            case "交易频次":
                // 交易频次逻辑处理
                break;
            case "交易金额":
                // 交易金额逻辑处理
                break;
            case "交易数量":
                // 交易数量逻辑处理
                break;
            case "合作年限":
                // 合作年限逻辑处理
                break;
            case "结算率":
                // 结算率逻辑处理
                break;
            default:
                break;
        }
        return imgConditionScore;
    }

}
